package psn.zjk.cacheTest;

import org.apache.commons.lang3.RandomStringUtils;

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @USER white
 * @DATETIME 2020/10/24 10:28
 */
public class CacheAsidePatternTest implements Runnable{

    private static RedisCache redisCache=new RedisCache();
    private DataBase db=new DataBase();
    private Random random=new Random();

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.execute(new CacheAsidePatternTest());
        executorService.execute(new CacheAsidePatternTest());
        executorService.execute(() -> {
            while (true){
                System.out.printf(String.valueOf(redisCache.get("key")));
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
            }
        });
    }


    @Override
    public void run() {
        long id = Thread.currentThread().getId();
        String random = RandomStringUtils.random(5);
        db.put("key",random);
        System.out.println("DB value："+random);
        if (this.random.nextInt(10)==1){
            return;
        }
        redisCache.put("key",random);
        System.out.println(MessageFormat.format("【{0}】:{1}",Thread.currentThread().getId()));
    }



}
